<?php

namespace app\api\controller;

use app\api\model\Product;
use think\Controller;
use think\Request;
use app\api\model\ShopModel;

class Shop extends Common {

    /**
     * 商城 -----商品列表
     * @param Request $request
     * @param ShopModel $model
     * @return \think\response\Json
     */
    public function upperList(Request $request, ShopModel $model) {
        $data = $request->param();
        $where = [];
        $whereC = [];
        $whereP = [];
        if (!empty($data['name'])) {
            $whereC[] = ['name', 'like', '%' . $data['name'] . '%'];
        }
        if (!empty($data['item'])) {
            $whereC[] = ['item', 'like', '%' . $data['item'] . '%'];
        }
        if (!empty($data['goods_code'])) {
            $whereC[] = ['goods_code', 'like', '%' . $data['goods_code'] . '%'];
        }
        if (!empty($data['min']) && !empty($data['max'])) {
            $where[] = ['price', 'between', [$data['min'], $data['max']]];
        }
        if (!empty($data['upper_status'])) {
            $where[] = ['status', '=', $data['upper_status']];
        }
        if (!empty($data['type'])) {
            $where[] = ['type', '=', $data['type']];
        }
        if (!empty($data['brand'])) {
            $whereC[] = ['brand', '=', $data['brand']];
        }
        if (!empty($data['tag'])) {
            $where[] = ['tag', '=', $data['tag']];
        }
        if (!empty($data['goods_status'])) {
            $whereC[] = ['status', '=', $data['goods_status']];
        }
        $commodity = \app\api\model\Commodity::where($whereC)->fetchCollection()->column('id');
        $product = ShopModel::where($where)->fetchCollection()->column('product_id');
        $list = Product::where($whereP)->where('commodity_id', 'IN', $commodity)
                        ->whereOr('id', 'IN', $product, 'OR')
                        ->with('commodity,shop')
//            ->order(['sort'=>'asc'])
                        ->getModel()->paginatePassPage($request->param('page', 1), $request->param('size', 10));
        if (!empty($list)) {
            $list = $list->toArray();
            if (!empty($list['data'])) {
                foreach ($list['data'] as &$val) {
                    if (!empty($val['shop'])) {
                        $val['shop']['status_name'] = $val['shop']['status'] ? "已下架" : "已上架";
                        $val['shop']['sex_name'] = $val['shop']['sex'] ? ($val['shop']['sex'] == 1 ? "女款" : "通款") : "男款";
                    }
                }
                unset($val);
            }
            return $this->returnJson(200, '成功', $list);
        } else {
            return $this->returnJson(500, '失败', []);
        }
    }

    /**
     * 商城上架商品列表
     * @param Request $request
     * @param ShopModel $model
     */
    public function upperListNew(Request $request, ShopModel $model) {
        $param = $request->param();
        $page = isset($param['page']) ? $param['page'] : 1;
        $size = isset($param['size']) ? $param['size'] : 10;
        $name_item = isset($param['name_item']) ? $param['name_item'] : '';
        $sale_status = isset($param['sale_status']) ? $param['sale_status'] : 2; //上架状态：0-已上架，1-已下架
        $brand = isset($param['brand']) ? $param['brand'] : '';
        $tag = isset($param['tag']) ? $param['tag'] : '';
        $com_status = isset($param['com_status']) ? $param['com_status'] : 2; //商品状态：0-禁用，1-启用
        $min_price = isset($param['min_price']) ? $param['min_price'] : '';
        $max_price = isset($param['max_price']) ? $param['max_price'] : '';
        $where = '';
        if (!empty($name_item)) {
            $where .= 'c.name like "%' . $name_item . '%" or c.item like "%' . $name_item . '%" or c.goods_code like "%' . $name_item . '%" and ';
        }
        if ($sale_status != 2) {
            $where .= 's.status = ' . $sale_status . ' and ';
        }
        if (!empty($brand)) {
            $where .= 'c.brand = ' . $brand . ' and ';
        }
        if (!empty($tag)) {
            $where .= 's.tag like "%' . $tag . '%" and ';
        }
        if ($com_status != 2) {
            $where .= 'c.status = ' . $com_status . ' and ';
        }
        if (!empty($min_price)) {
            $where .= 'c.retail_price >= ' . $min_price . ' and ';
        }
        if (!empty($max_price)) {
            $where .= 'c.retail_price <= ' . $max_price . ' and ';
        }
        if (!empty($where)) {
            $search = mb_substr($where, 0, -5);
        } else {
            $search = '1=1';
        }
        $data = $model->getUpperListNew($page, $size, $search);
        return $this->returnJson(200, '成功', $data);
    }

    /**
     * 获取商品标签
     */
    public function getGoodsTags() {
        $data = [
                ['id' => 1, 'name' => '精品'],
                ['id' => 2, 'name' => '热卖'],
                ['id' => 3, 'name' => '新品'],
        ];
        return $this->returnJson(200, '成功', $data);
    }

    /**
     * 商城 ------上架商品
     * @param $id
     * @param ShopModel $model
     * @return \think\response\Json
     */
    public function upperGoods(Request $request, ShopModel $model) {
        $id_str = $request->param('id_list');
        if (empty($id_str)) {
            return $this->returnJson(500, 'id_list不能为空');
        }
        $id_list = explode(',', $id_str);
        $data = $model->batchRackingGoods($id_list);
        return $this->returnJson(200, '上架成功', $data);
    }

    /**
     * 商城------下架商品
     * @param Request $request
     * @param ShopModel $model
     */
    public function downGoods(Request $request, ShopModel $model) {
        $id_str = $request->param('id_list');
        if (empty($id_str)) {
            return $this->returnJson(500, 'id_list不能为空');
        }
        $id_list = explode(',', $id_str);
        $data = $model->batchSoldoutGoods($id_list);
        return $this->returnJson(200, '下架成功', $data);
    }

    /**
     * 订单管理  ----列表
     * @param Request $request
     * @param ShopModel $model
     * @return \think\response\Json
     */
    public function orderList(Request $request, ShopModel $model) {
        $param = $request->param();
        $page = isset($param['page']) ? $param['page'] : 1;
        $size = isset($param['size']) ? $param['size'] : 10;
        $status = isset($param['status']) ? $param['status'] : 9; //订单状态：0-待付款，1-待确认，2-待出售，3-待收货，4-已完成，5-已撤销，9-全部
        $member = isset($param['member']) ? $param['member'] : ''; //会员昵称
        $order_num = isset($param['order_num']) ? $param['order_num'] : ''; //订单编号
        $start_time = isset($param['start_time']) ? strtotime($param['start_time']) : 0; //自定义时传的起始时间
        $end_time = isset($param['end_time']) ? strtotime($param['end_time']) : 0; //自定义时传的截止时间
        //处理搜索条件
        $where = '';
        if ($status != 9) {
            $where .= 'o.status = ' . $status . ' and ';
        }
        if (!empty($member)) {
            $where .= 'm.shop_name like "%' . $member . '%" and ';
        }
        if (!empty($order_num)) {
            $where .= 'o.order_num like "%' . $order_num . '%" and ';
        }
        if (!empty($start_time)) {
            $where .= 'o.create_time >= ' . $start_time . ' and ';
        }
        if (!empty($end_time)) {
            $where .= 'o.create_time <= ' . $end_time . ' and ';
        }
        if (!empty($where)) {
            $search = mb_substr($where, 0, -5);
        } else {
            $search = '1=1';
        }
        $data = $model->getOrderList($page, $size, $search);
        return $this->returnJson(200, '成功', $data);
    }

    /**
     * 订单管理  ----详情
     * @param $id
     * @param ShopModel $model
     * @return \think\response\Json
     */
    public function orderInfo($id, ShopModel $model) {
        $list = $model->getOrderInfo($id);
        if (!empty($list)) {
            return $this->returnJson(200, '成功', $list);
        } else {
            return $this->returnJson(500, '失败', []);
        }
    }

    /**
     * 订单管理  ----撤销
     * @param $id
     * @param ShopModel $model
     * @return \think\response\Json
     */
    public function orderRevoke($id, ShopModel $model) {
        $list = $model->getOrderRevoke($id);
        if ($list == 1) {
            return $this->returnJson(200, '成功');
        } else {
            return $this->returnJson(500, '商品正在出库,无法撤销');
        }
    }

    /**
     * 订单管理  ----转销售单
     * @param $id
     * @param ShopModel $model
     * @return \think\response\Json
     */
    public function OrderTurn($id, ShopModel $model) {
        $list = $model->getOrderTurn($id);
        if ($list == 1) {
            return $this->returnJson(200, '成功');
        } else {
            return $this->returnJson(500, '失败');
        }
    }

    /**
     * 订单管理  ----改价
     * @param Request $request
     * @param ShopModel $model
     * @return \think\response\Json
     */
    public function OrderRevision(Request $request, ShopModel $model) {
        $option = $request->param('option');
//        $option = json_decode($option, true);
        $data = $model->getOrderRevision($option);
        if ($data) {
            return $this->returnJson(200, '成功');
        } else {
            return $this->returnJson(500, '失败');
        }
    }

    /**
     * 微商城 ----会员列表
     * @param Request $request
     * @param ShopModel $model
     * @return \think\response\Json
     */
    public function memberList(Request $request, ShopModel $model) {
        $param = $request->param();
        $page = isset($param['page']) ? $param['page'] : 1;
        $size = isset($param['size']) ? $param['size'] : 10;
        $name_phone = isset($param['name_phone']) ? $param['name_phone'] : '';
        $status = isset($param['status']) ? $param['status'] : 2; //关联状态：0-未关联，1-已关联，2-全部
        $where = '';
        if (!empty($name_phone)) {
            if (is_numeric($name_phone)) {
                $where .= 'shop_phone like "%' . $name_phone . '%" and ';
            } else {
                $where .= 'nick_name like "%' . $name_phone . '%" and ';
            }
        }
        if ($status != 2) {
            $where .= 'status = ' . $status . ' and ';
        }
        if (!empty($where)) {
            $search = mb_substr($where, 0, -5);
        } else {
            $search = '1=1';
        }
        $data = $model->getMemberList($page, $size, $search);
        return $this->returnJson(200, '成功', $data);
    }

    /**
     * 微商城 ----查看会员信息
     * @param Request $request
     * @param ShopModel $model
     * @return \think\response\Json
     */
    public function memberInfo($id, ShopModel $model) {
        $list = $model->getMemberInfo($id);
        if (!empty($list)) {
            return $this->returnJson(200, '成功', $list);
        } else {
            return $this->returnJson(500, '失败');
        }
    }

    /**
     * 微商城 ----关联客户
     * @param Request $request
     * @param ShopModel $model
     * @return \think\response\Json
     */
    public function memberRelation(Request $request, ShopModel $model) {
        $param = $request->param();
        $member_id = isset($param['member_id']) ? $param['member_id'] : 0; //会员id
        $user_id = isset($param['user_id']) ? $param['user_id'] : 0; //客户id
        $data = $model->getMemberRelation($member_id, $user_id);
        if ($data) {
            return $this->returnJson(200, '成功');
        } else {
            return $this->returnJson(500, '已关联,请勿重复操作');
        }
    }

    /**
     * 微商城 ----解除关联
     * @param Request $request
     * @param ShopModel $model
     * @return \think\response\Json
     */
    public function memberMove(Request $request, ShopModel $model) {
        $list = $model->getMemberMove($request->param('id'));
        if ($list == 1) {
            return $this->returnJson(200, '成功');
        } else {
            return $this->returnJson(500, '已经解除关联,请勿重复操作');
        }
    }

    /**
     * 店铺概况 ----查看店铺信息
     * @param Request $request
     * @param ShopModel $model
     * @return \think\response\Json
     */
    public function storeStateInfo(ShopModel $model) {
        $id = 1; //店铺只有一个，直接取固定id
        $data = $model->getStoreStateInfo($id);
        return $this->returnJson(200, '成功', $data);
    }

    /**
     * 店铺概况 ----修改店铺信息
     * @param Request $request
     * @param ShopModel $model
     * @return \think\response\Json
     */
    public function storeStateEdit(Request $request, ShopModel $model) {
        $id = 1; //店铺只有一个，直接取固定id
        $param = $request->param();
        $data = [];
        if (isset($param['store_name']) && !empty($param['store_name'])) {
            $data['store_name'] = $param['store_name'];
        }
        if (isset($param['store_des']) && !empty($param['store_des'])) {
            $data['store_des'] = $param['store_des'];
        }
        if (isset($param['tel']) && !empty($param['tel'])) {
            $data['tel'] = $param['tel'];
        }
        if (isset($param['merchant']) && !empty($param['merchant'])) {
            $data['merchant'] = $param['merchant'];
        }
        if (isset($param['address']) && !empty($param['address'])) {
            $data['address'] = $param['address'];
        }
        if (isset($param['qr']) && !empty($param['qr'])) {
            $data['qr'] = $param['qr'];
        }
        $list = $model->getStoreStateEdit($id, $data);
        if (!empty($list)) {
            return $this->returnJson(200, '成功');
        } else {
            return $this->returnJson(500, '失败');
        }
    }

    /**
     * banner图 ----- 列表
     * @param ShopModel $model
     * @return \think\response\Json
     */
    public function bannerList(ShopModel $model) {
        $list = $model->getBannerList();
        return $this->returnJson(200, '成功', $list);
    }

    /**
     * banner图 ----- 新增
     * @param ShopModel $model
     * @return \think\response\Json
     */
    public function AddShopBanner(Request $request, ShopModel $model) {
        $param = $request->param();
        $images = isset($param['images']) ? $param['images'] : '';
        $shop_id = isset($param['shop_id']) ? $param['shop_id'] : '';
        $shop_name = isset($param['shop_name']) ? $param['shop_name'] : '';
        if (empty($images) || empty($shop_id) || empty($shop_name)) {
            return json(['code' => 500, 'msg' => '缺少必要参数']);
        }
        $data = $model->getBannerAdd(['images' => current($images), 'shop_id' => $shop_id, 'shop_name' => $shop_name]);
        if ($data) {
            return json(['code' => 200, 'msg' => '成功']);
        } else {
            return json(['code' => 500, 'msg' => '失败']);
        }
    }

    /**
     * banner图 ----- 修改
     * @param ShopModel $model
     * @return \think\response\Json
     */
    public function EditShopBanner(Request $request, ShopModel $model) {
        $param = $request->param();
        $id = isset($param['id']) ? $param['id'] : 0;
        if (empty($id)) {
            return json(['code' => 500, 'msg' => 'id不能为空']);
        }
        $data = [];
        if (isset($param['images']) && !empty($param['images'])) {
            $data['images'] = current($param['images']);
        }
        if (isset($param['shop_id']) && !empty($param['shop_id'])) {
            $data['shop_id'] = $param['shop_id'];
        }
        if (isset($param['shop_name']) && !empty($param['shop_name'])) {
            $data['shop_name'] = $param['shop_name'];
        }
        if (empty($data)) {
            return json(['code' => 500, 'msg' => '参数不能为空']);
        }
        $list = $model->getBannerEdit($id, $data);
        if (!empty($list)) {
            return json(['code' => 200, 'msg' => '成功']);
        } else {
            return json(['code' => 500, 'msg' => '失败']);
        }
    }

    /**
     * banner图 ----- 删除
     * @param ShopModel $model
     * @return \think\response\Json
     */
    public function DelShopBanner(Request $request, ShopModel $model) {
        $list = $model->getBannerDel($request->param('id'));
        if (!empty($list)) {
            return json(['code' => 200, 'msg' => '成功']);
        } else {
            return json(['code' => 500, 'msg' => '失败']);
        }
    }

    /**
     * banner图 ----- 查找商品
     * @param ShopModel $model
     * @return \think\response\Json
     */
//    public function bannerGoods(Request $request, ShopModel $model) {
//        $data = $request->param();
//        $data['name'] = $data['name'] ?? '';
//        $list = $model->getBannerGoods($data);
//        if (!empty($list)) {
//            return $this->returnJson(200, '成功', $list);
//        } else {
//            return $this->returnJson(500, '失败', []);
//        }
//    }

    /**
     * banner图 ----- 排序
     * @param ShopModel $model
     * @return \think\response\Json
     */
    public function shopBannerSort(Request $request, ShopModel $model) {
        $param = $request->param();
        $id = isset($param['id']) ? $param['id'] : 0;
        $sort = isset($param['sort']) ? $param['sort'] : ''; //排序：上移-asc,下移-desc
        if (empty($id) || empty($sort)) {
            return $this->returnJson(500, '参数不能为空');
        }
        $data = $model->editBannerSort($id, $sort);
        if ($data) {
            return $this->returnJson(200, '成功');
        } else {
            return $this->returnJson(201, '已是最佳排序');
        }
    }

    /**
     * 搜索进销存客户列表
     * @param Request $request
     * @param ShopModel $model
     */
    public function searchUserList(Request $request, ShopModel $model) {
        $param = $request->param();
        $page = isset($param['page']) ? $param['page'] : 1;
        $size = isset($param['size']) ? $param['size'] : 10;
        $name_phone = isset($param['name_phone']) ? $param['name_phone'] : '';
        $where = '';
        if (!empty($name_phone)) {
            if (is_numeric($name_phone)) {
                $where = 'phone like "%' . $name_phone . '%"';
            } else {
                $where = 'name like "%' . $name_phone . '%"';
            }
        }
        $data = $model->getSearchUserList($page, $size, $where);
        return $this->returnJson(200, '成功', $data);
    }

    /**
     * 查找上架商品
     * @param Request $request
     * @param ShopModel $model
     */
    public function getShopList(Request $request, ShopModel $model) {
        $param = $request->param();
        $page = isset($param['page']) ? $param['page'] : 1;
        $size = isset($param['size']) ? $param['size'] : 10;
        $name = isset($param['name']) ? $param['name'] : ''; //商品名称
        $category = isset($param['category']) ? $param['category'] : ''; //分类
        $brand = isset($param['brand']) ? $param['brand'] : ''; //品牌
        $tag = isset($param['tag']) ? $param['tag'] : ''; //标签
        $freight_temp = isset($param['freight_temp']) ? $param['freight_temp'] : ''; //运费模板
        $where = '';
        if (!empty($name)) {
            $where .= 'name like "%' . $name . '%" and ';
        }
        if (!empty($category)) {
            $where .= 'category = ' . $category . ' and ';
        }
        if (!empty($brand)) {
            $where .= 'brand = ' . $brand . ' and ';
        }
        if (!empty($tag)) {
            $where .= 'tag = ' . $tag . ' and ';
        }
        if (!empty($freight_temp)) {
            $where .= 'type = ' . $freight_temp . ' and ';
        }
        if (!empty($where)) {
            $search = mb_substr($where, 0, -5);
        } else {
            $search = '1=1';
        }
        $data = $model->getOnshopnList($page, $size, $search);
        return $this->returnJson(200, '成功', $data);
    }

    /**
     * 省，市
     * @param Request $request
     */
    public function getAreaList(Request $request) {
        $param = $request->param();
        $pid = isset($param['pid']) ? $param['pid'] : 0; //省份id
        $data = db('area')->where(['pid' => $pid])->field('id,name')->select();
        return $this->returnJson(200, '成功', $data);
    }

    /**
     * 新增运费模板
     * @param Request $request
     * @param ShopModel $model
     */
    public function addFreight(Request $request, ShopModel $model) {
        $param = $request->param();
        $data = [];
        $data['name'] = isset($param['name']) ? $param['name'] : '';
        $data['is_df'] = isset($param['is_df']) ? $param['is_df'] : 0;
        $data['df_first_num'] = isset($param['df_first_num']) ? $param['df_first_num'] : 0;
        $data['df_first_cost'] = isset($param['df_first_cost']) ? $param['df_first_cost'] : 0.00;
        $data['df_add_num'] = isset($param['df_add_num']) ? $param['df_add_num'] : 0;
        $data['df_add_cost'] = isset($param['df_add_cost']) ? $param['df_add_cost'] : 0.00;
        $data['df_free_amount'] = isset($param['df_free_amount']) ? $param['df_free_amount'] : 0.00;
        $data['area_list'] = isset($param['area_list']) ? $param['area_list'] : '';
        $data['goods_list'] = isset($param['goods_list']) ? $param['goods_list'] : '';
        $data['first_num'] = isset($param['first_num']) ? $param['first_num'] : 0;
        $data['first_cost'] = isset($param['first_cost']) ? $param['first_cost'] : 0.00;
        $data['add_num'] = isset($param['add_num']) ? $param['add_num'] : 0;
        $data['add_cost'] = isset($param['add_cost']) ? $param['add_cost'] : 0.00;
        $data['free_amount'] = isset($param['free_amount']) ? $param['free_amount'] : 0.00;
        $res = $model->addFreight($data);
        if ($res !== FALSE) {
            return $this->returnJson(200, '成功');
        } else {
            return $this->returnJson(500, '失败');
        }
    }

    /**
     * 编辑运费模板
     * @param Request $request
     * @param ShopModel $model
     */
    public function editFreight(Request $request, ShopModel $model) {
        $param = $request->param();
        $id = isset($param['id']) ? $param['id'] : 0;
        if (empty($id)) {
            return $this->returnJson(500, '参数id不能为空');
        }
        $data = [];
        if (isset($param['name']) && !empty($param['name'])) {
            $data['name'] = $param['name'];
        }
        if (isset($param['is_df'])) {
            $data['is_df'] = $param['is_df'];
        }
        if (isset($param['df_first_num'])) {
            $data['df_first_num'] = $param['df_first_num'];
        }
        if (isset($param['df_first_cost'])) {
            $data['df_first_cost'] = $param['df_first_cost'];
        }
        if (isset($param['df_add_num'])) {
            $data['df_add_num'] = $param['df_add_num'];
        }
        if (isset($param['df_add_cost'])) {
            $data['df_add_cost'] = $param['df_add_cost'];
        }
        if (isset($param['df_free_amount'])) {
            $data['df_free_amount'] = $param['df_free_amount'];
        }
        if (isset($param['area_list']) && !empty($param['area_list'])) {
            $data['area_list'] = $param['area_list'];
        }
        if (isset($param['first_num'])) {
            $data['first_num'] = $param['first_num'];
        }
        if (isset($param['first_cost'])) {
            $data['first_cost'] = $param['first_cost'];
        }
        if (isset($param['add_num'])) {
            $data['add_num'] = $param['add_num'];
        }
        if (isset($param['add_cost'])) {
            $data['add_cost'] = $param['add_cost'];
        }
        if (isset($param['free_amount'])) {
            $data['free_amount'] = $param['free_amount'];
        }

        $res = $model->editFreight($id, $data);
        if ($res !== FALSE) {
            return $this->returnJson(200, '成功');
        } else {
            return $this->returnJson(500, '失败');
        }
    }

    /**
     * 删除运费模板
     * @param Request $request
     * @param ShopModel $model
     */
    public function delFreight(Request $request, ShopModel $model) {
        $param = $request->param();
        $id = isset($param['id']) ? $param['id'] : 0;
        if (empty($id)) {
            return $this->returnJson(500, '参数id不能为空');
        }
        $res = $model->delFreight($id);
        if ($res !== FALSE) {
            return $this->returnJson(200, '成功');
        } else {
            return $this->returnJson(500, '失败');
        }
    }

    /**
     * 获取运费模板列表
     * @param ShopModel $model
     */
    public function getFreightList(ShopModel $model) {
        $data = $model->getFreightList();
        return $this->returnJson(200, '成功', $data);
    }

    /**
     * 批量设置商品标签
     * @param Request $request
     * @param ShopModel $model
     */
    public function batchShopTag(Request $request, ShopModel $model) {
        $param = $request->param();
        $type = isset($param['type']) ? $param['type'] : 0; //标签类型：1-精品，2-热卖，3-新品
        $goods_id = isset($param['goods_id']) ? $param['goods_id'] : 0; //设置的商品id
        if (empty($type) || empty($goods_id)) {
            return $this->returnJson(500, '参数缺失');
        }
        $tag = '';
        switch ($type) {
            case 1:
                $tag = '精品';
                break;
            case 2:
                $tag = '热卖';
                break;
            case 3:
                $tag = '新品';
                break;
            default:
                break;
        }
        $goods_list = explode(',', $goods_id);
        $data = $model->batchShopTag($tag, $goods_list);
        return $this->returnJson(200, '成功', $data);
    }

    /**
     * 销量/价格展示
     * @param Request $request
     * @param ShopModel $model
     */
    public function salePriceShow(Request $request, ShopModel $model) {
        $param = $request->param();
        $data = [];
        if (isset($param['sale_show'])) {//销量是否展示：0-否，1-是
            $data['sale_show'] = $param['sale_show'];
        }
        if (isset($param['retail_price'])) {//价格是否零售：0-否，1-是
            $data['retail_price'] = $param['retail_price'];
        }
        if (empty($data)) {
            return $this->returnJson(500, '参数缺失');
        }
        $res = $model->editSystemConfig('销量/价格展示', 'sale_price_show', $data);
        if ($res !== FALSE) {
            return $this->returnJson(200, '成功');
        } else {
            return $this->returnJson(500, '失败');
        }
    }

    /**
     * 库存配置
     * @param Request $request
     * @param ShopModel $model
     */
    public function repertoryConfig(Request $request, ShopModel $model) {
        $param = $request->param();
        $status = isset($param['status']) ? $param['status'] : 0; //库存状态：0-启用，1-停用
        $count_type = isset($param['count_type']) ? $param['count_type'] : 0; //库存计数：0-当前库存，1-可用库存
        $sub_count = isset($param['sub_count']) ? $param['sub_count'] : 0; //是否订单付款后减库存：0-否，1-是
        $data = $model->editSystemConfig('库存配置', 'repertory_config', ['status' => $status, 'count_type' => $count_type, 'sub_count' => $sub_count]);
        if ($data !== FALSE) {
            return $this->returnJson(200, '成功');
        } else {
            return $this->returnJson(500, '失败');
        }
    }

    /**
     * 上架商品积分规则
     * @param Request $request
     * @param ShopModel $model
     */
    public function getShopRule(Request $request, ShopModel $model) {
        $data = $model->getShopRuleConfig();
        return $this->returnJson(200, '成功', $data);
    }

    /**
     * 批量设置商品运费模板
     * @param Request $request
     * @param ShopModel $model
     */
    public function batchShopFreight(Request $request, ShopModel $model) {
        $param = $request->param();
        $id = isset($param['id']) ? $param['id'] : 0; //模板id
        $goods_id = isset($param['goods_id']) ? $param['goods_id'] : -1; //商品id集合,应用至全部商品时传0
        if (empty($id) || $goods_id == -1) {
            return $this->returnJson(500, '参数缺失');
        }
        $data = $model->batchShopFreight($id, $goods_id);
        return $this->returnJson(200, '成功', $data);
    }

    /**
     * 获取运费模板详情
     * @param type $id
     * @param ShopModel $model
     */
    public function getFreightOne($id, ShopModel $model) {
        $data = $model->getFreightInfo($id);
        return $this->returnJson(200, '成功', $data);
    }

    /**
     * 设置默认运费模板
     * @param Request $request
     * @param ShopModel $model
     */
    public function setDefaultFreight(Request $request, ShopModel $model) {
        $param = $request->param();
        $id = isset($param['id']) ? $param['id'] : 0; //模板id
        $data = $model->setDefaultFreight($id);
        if ($data !== FALSE) {
            return $this->returnJson(200, '成功');
        } else {
            return $this->returnJson(500, '失败');
        }
    }

    /**
     * APP商城订单列表
     * @param Request $request
     * @param ShopModel $model
     */
    public function appOrderList(Request $request, ShopModel $model) {
        $param = $request->param();
        $page = isset($param['page']) ? $param['page'] : 1;
        $size = isset($param['size']) ? $param['size'] : 10;
        $status = isset($param['status']) ? $param['status'] : 0;
        $list = $model->appGetOrderList($page, $size, ['o.status' => $status]);
        return $this->returnJson(200, '成功', $list);
    }

    /**
     * APP领取会员卡
     * @param Request $request
     * @param ShopModel $model
     */
    public function collectMemberCard(Request $request, ShopModel $model) {
        $param = $request->param();
        $member_id = isset($param['member_id']) ? $param['member_id'] : 0;
        if (empty($member_id)) {
            return $this->returnJson(500, '参数缺失');
        }
        $data = $model->getMemberCard($member_id);
        if ($data) {
            return $this->returnJson(200, '领取成功');
        } else {
            return $this->returnJson(500, '不可重复领取');
        }
    }

    /**
     * APP已领取优惠券列表
     * @param Request $request
     * @param ShopModel $model
     */
    public function appGetedCouponList(Request $request, ShopModel $model) {
        $param = $request->param();
        $page = isset($param['page']) ? $param['page'] : 1;
        $size = isset($param['size']) ? $param['size'] : 10;
        $member_id = isset($param['member_id']) ? $param['member_id'] : 0;
        if (empty($member_id)) {
            return $this->returnJson(500, '参数缺失');
        }
        $list = $model->getCouponList($page, $size, $member_id);
        return $this->returnJson(200, '成功', $list);
    }

    /**
     * APP未领取优惠券列表
     * @param Request $request
     * @param ShopModel $model
     */
    public function appUngetCouponList(Request $request, ShopModel $model) {
        $param = $request->param();
        $page = isset($param['page']) ? $param['page'] : 1;
        $size = isset($param['size']) ? $param['size'] : 10;
        $member_id = isset($param['member_id']) ? $param['member_id'] : 0;
        if (empty($member_id)) {
            return $this->returnJson(500, '参数缺失');
        }
        $list = $model->getUngetCouponList($page, $size, $member_id);
        return $this->returnJson(200, '成功', $list);
    }

    /**
     * APP领取优惠券
     * @param Request $request
     * @param ShopModel $model
     */
    public function collectCoupon(Request $request, ShopModel $model) {
        $param = $request->param();
        $id = isset($param['id']) ? $param['id'] : 0;
        $member_id = isset($param['member_id']) ? $param['member_id'] : 0;
        if (empty($id) || empty($member_id)) {
            return $this->returnJson(500, '参数缺失');
        }
        $data = $model->collectCoupon($id, $member_id);
        if ($data) {
            return $this->returnJson(200, '领取成功');
        } else {
            return $this->returnJson(500, '不可重复领取');
        }
    }

    /**
     * APP积分
     * @param Request $request
     * @param ShopModel $model
     */
    public function appIntegral(Request $request, ShopModel $model) {
        $param = $request->param();
        $member_id = isset($param['member_id']) ? $param['member_id'] : 0;
        if (empty($member_id)) {
            return $this->returnJson(500, '参数缺失');
        }
        $data = $model->getMemberIntegral($member_id);
        return $this->returnJson(200, '成功', $data);
    }

    /**
     * 打印
     * @param Request $request
     * @param ShopModel $model
     */
    public function printNote(Request $request, ShopModel $model) {
        $param = $request->param();
        $tim = isset($param['tim']) ? $param['tim'] : ''; //终端：web端-pc，APP-app
        $type = isset($param['type']) ? $param['type'] : ''; //销售单-sale，退货单-retreat，采购单-purchase，配货单-cargo,采购退货单-purchase_retreat,销售前台收银历史-sale_cash,已出库单-outstock_out,已入库单-outstock_in
        $id = isset($param['id']) ? $param['id'] : 0;
        if (empty($tim) || empty($type) || empty($id)) {
            return $this->returnJson(500, '参数缺失');
        }
        //获取渲染数据
        $render_data = $this->getTmpData($type, $id);
        if ($tim == 'pc') {
            $render_data = $render_data['pc'];
            //获取模板内容---前半部
            $tmp_header_str = file_get_contents('./template/header.html');
            $tmp_header = sprintf($tmp_header_str, $render_data['title'], $render_data['user_name'], $render_data['bills_num'], $render_data['mobile'], $render_data['ctime']);
            //商品信息
            $goods_html = '';
            if (!empty($render_data['goods_list'])) {
                for ($i = 0; $i < count($render_data['goods_list']); $i++) {
                    $goods_html .= '<tr><td>' . ($i + 1) . '</td><td>' . $render_data['goods_list'][$i]['name'] . '</td><td>' . $render_data['goods_list'][$i]['item'] . '</td>'
                            . '<td>' . $render_data['goods_list'][$i]['color'] . '</td><td>' . $render_data['goods_list'][$i]['size'] . '</td>'
                            . '<td>' . $render_data['goods_list'][$i]['num'] . '</td><td>' . $render_data['goods_list'][$i]['unit'] . '</td>'
                            . '<td>' . $render_data['goods_list'][$i]['price'] . '</td><td>' . $render_data['goods_list'][$i]['discount'] . '</td>'
                            . '<td>' . $render_data['goods_list'][$i]['disprice'] . '</td><td>' . $render_data['goods_list'][$i]['money'] . '</td>'
                            . '<td>' . $render_data['goods_list'][$i]['dismoney'] . '</td></tr>';
                }
            }
            //获取模板内容---后半部
            $tmp_footer_str = file_get_contents('./template/footer.html');
            $tmp_footer = sprintf($tmp_footer_str, $render_data['total'], $render_data['money'], $render_data['dismoney'], $render_data['sure_money'], $render_data['actual_money'], $render_data['arrears'], $render_data['maker_name'], $render_data['warehouse_name'], $render_data['remark']);
            return $this->returnJson(200, '成功', ['template_str' => $tmp_header . $goods_html . $tmp_footer]);
        } else {
            return $this->returnJson(200, '成功', $render_data['app']);
        }
    }

    /**
     * 获取模板渲染数据
     * @param type $type
     * @param type $id
     */
    private function getTmpData($type, $id) {
        $dataPc = $dataApp = [];
        switch ($type) {
            case 'sale'://销售单
                $model = new \app\api\model\SaleModel;
                $info = $model->getSaleInfo($id);
                //标题
                $title = '销售单';
                //商品信息
                $goods_list = [];
                if (!empty($info['goods'])) {
                    foreach ($info['goods'] as $key => $val) {
                        $goods_list[$key]['name'] = $val['name'];
                        $goods_list[$key]['num'] = strval($val['put_number']);
                        $goods_list[$key]['price'] = strval($val['price']);
                        $goods_list[$key]['money'] = strval($val['money']);
                        $goods_list[$key]['item'] = $val['item'];
                        $goods_list[$key]['color'] = $val['color'];
                        $goods_list[$key]['size'] = $val['size'];
                        $goods_list[$key]['unit'] = $val['unit'];
                        $goods_list[$key]['discount'] = strval($val['discount']);
                        $goods_list[$key]['dismoney'] = strval($val['dismoney']);
                        $goods_list[$key]['disprice'] = strval($val['disprice']);
                    }
                }
                $total_num = strval(array_sum(array_column($goods_list, 'num')));
                $total_money = strval(array_sum(array_column($goods_list, 'money')));
                $total_dismoney = strval(array_sum(array_column($goods_list, 'dismoney')));
                //APP数据
                $dataApp['title'] = $title;
                if (!empty($info['kid_name'])) {
                    $dataApp['info_list'][] = '客户：' . $info['kid_name'];
                }
                if (!empty($info['kid_phone'])) {
                    $dataApp['info_list'][] = '联系方式：' . $info['kid_phone'];
                }
                if (!empty($info['maker_name'])) {
                    $dataApp['info_list'][] = '制单人：' . $info['maker_name'];
                }
                if (!empty($info['warehouse_name'])) {
                    $dataApp['info_list'][] = '仓库：' . $info['warehouse_name'];
                }
                if (!empty($info['number'])) {
                    $dataApp['info_list'][] = '单号：' . $info['number'];
                }
                if (!empty($info['create_time'])) {
                    $dataApp['info_list'][] = '业务日期：' . $info['create_time'];
                }
                $dataApp['info_list'][] = '____________________________';
                $dataApp['goods_title'] = ['商品', '数量', '单价', '金额'];
                $dataApp['goods_list'] = $goods_list;
                $dataApp['footer_line'] = '____________________________';
                $dataApp['total'] = ['合计', $total_num, $total_money];
                //PC数据
                $dataPc['title'] = $title;
                $dataPc['user_name'] = $info['kid_name']; //客户
                $dataPc['mobile'] = $info['kid_phone']; //手机号
                $dataPc['maker_name'] = $info['maker_name']; //制单人
                $dataPc['warehouse_name'] = $info['warehouse_name']; //仓库
                $dataPc['sure_money'] = $info['total']; //应收金额
                $dataPc['actual_money'] = $info['payment']; //应收金额
                $dataPc['bills_num'] = $info['number']; //单号
                $dataPc['ctime'] = $info['create_time']; //日期
                $dataPc['remark'] = $info['des']; //备注
                $dataPc['goods_list'] = $goods_list; //商品列表
                $dataPc['total'] = $total_num; //数量
                $dataPc['money'] = $total_money; //金额
                $dataPc['dismoney'] = $total_dismoney; //折后金额
                $dataPc['arrears'] = $info['arrears']; //欠款
                break;
            case 'retreat'://销售退货单
                $model = new \app\api\model\SaleModel;
                $info = $model->getReturnDetail($id);
                //标题
                $title = '退货单';
                //商品信息
                $goods_list = [];
                if (!empty($info['goods'])) {
                    foreach ($info['goods'] as $key => $val) {
                        $goods_list[$key]['name'] = $val['name'];
                        $goods_list[$key]['num'] = strval($val['put_number']);
                        $goods_list[$key]['price'] = strval($val['price']);
                        $goods_list[$key]['money'] = strval($val['money']);
                        $goods_list[$key]['item'] = $val['item'];
                        $goods_list[$key]['color'] = $val['color'];
                        $goods_list[$key]['size'] = $val['size'];
                        $goods_list[$key]['unit'] = $val['unit'];
                        $goods_list[$key]['discount'] = strval($val['discount']);
                        $goods_list[$key]['dismoney'] = strval($val['discount_money']);
                        $goods_list[$key]['disprice'] = strval($val['discount_price']);
                    }
                }
                $total_num = strval(array_sum(array_column($goods_list, 'num')));
                $total_money = strval(array_sum(array_column($goods_list, 'money')));
                $total_dismoney = strval(array_sum(array_column($goods_list, 'dismoney')));
                //APP数据
                $dataApp['title'] = $title;
                if (!empty($info['kid_name'])) {
                    $dataApp['info_list'][] = '客户：' . $info['kid_name'];
                }
                if (!empty($info['kid_phone'])) {
                    $dataApp['info_list'][] = '联系方式：' . $info['kid_phone'];
                }
                if (!empty($info['maker_name'])) {
                    $dataApp['info_list'][] = '制单人：' . $info['maker_name'];
                }
                if (!empty($info['warehouse_name'])) {
                    $dataApp['info_list'][] = '仓库：' . $info['warehouse_name'];
                }
                if (!empty($info['number'])) {
                    $dataApp['info_list'][] = '单号：' . $info['number'];
                }
                if (!empty($info['create_time'])) {
                    $dataApp['info_list'][] = '业务日期：' . $info['create_time'];
                }
                $dataApp['info_list'][] = '____________________________';
                $dataApp['goods_title'] = ['商品', '数量', '单价', '金额'];
                $dataApp['goods_list'] = $goods_list;
                $dataApp['footer_line'] = '____________________________';
                $dataApp['total'] = ['合计', $total_num, $total_money];
                //PC数据
                $dataPc['title'] = $title;
                $dataPc['user_name'] = $info['kid_name']; //客户
                $dataPc['mobile'] = $info['kid_phone']; //手机号
                $dataPc['maker_name'] = $info['maker_name']; //制单人
                $dataPc['warehouse_name'] = $info['warehouse_name']; //仓库
                $dataPc['sure_money'] = $info['total']; //应收金额
                $dataPc['actual_money'] = $info['payment']; //应收金额
                $dataPc['bills_num'] = $info['number']; //单号
                $dataPc['ctime'] = $info['create_time']; //日期
                $dataPc['remark'] = $info['des']; //备注
                $dataPc['goods_list'] = $goods_list;
                $dataPc['total'] = $total_num; //数量
                $dataPc['money'] = $total_money; //金额
                $dataPc['dismoney'] = $total_dismoney; //折后金额
                $dataPc['arrears'] = ''; //欠款
                break;
            case 'purchase'://采购单
                $model = new \app\api\model\PurchaseModel;
                $info = $model->getPurchaseInfo($id);
                //标题
                $title = '采购单';
                //商品信息
                $goods_list = [];
                if (!empty($info['goods'])) {
                    foreach ($info['goods'] as $key => $val) {
                        $goods_list[$key]['name'] = $val['name'];
                        $goods_list[$key]['num'] = strval($val['put_number']);
                        $goods_list[$key]['price'] = strval($val['price']);
                        $goods_list[$key]['money'] = strval($val['money']);
                        $goods_list[$key]['item'] = $val['item'];
                        $goods_list[$key]['color'] = $val['color'];
                        $goods_list[$key]['size'] = $val['size'];
                        $goods_list[$key]['unit'] = $val['unit'];
                        $goods_list[$key]['discount'] = strval($val['discount']);
                        $goods_list[$key]['dismoney'] = strval($val['discount_money']);
                        $goods_list[$key]['disprice'] = strval($val['discount_price']);
                    }
                }
                $total_num = strval(array_sum(array_column($goods_list, 'num')));
                $total_money = strval(array_sum(array_column($goods_list, 'money')));
                $total_dismoney = strval(array_sum(array_column($goods_list, 'dismoney')));
                //APP数据
                $dataApp['title'] = $title;
                if (!empty($info['kid_name'])) {
                    $dataApp['info_list'][] = '客户：' . $info['kid_name'];
                }
                if (!empty($info['kid_phone'])) {
                    $dataApp['info_list'][] = '联系方式：' . $info['kid_phone'];
                }
                if (!empty($info['maker_name'])) {
                    $dataApp['info_list'][] = '制单人：' . $info['maker_name'];
                }
                if (!empty($info['warehouse_name'])) {
                    $dataApp['info_list'][] = '仓库：' . $info['warehouse_name'];
                }
                if (!empty($info['number'])) {
                    $dataApp['info_list'][] = '单号：' . $info['number'];
                }
                if (!empty($info['create_time'])) {
                    $dataApp['info_list'][] = '业务日期：' . $info['create_time'];
                }
                $dataApp['info_list'][] = '____________________________';
                $dataApp['goods_title'] = ['商品', '数量', '单价', '金额'];
                $dataApp['goods_list'] = $goods_list;
                $dataApp['footer_line'] = '____________________________';
                $dataApp['total'] = ['合计', $total_num, $total_money];
                //PC数据
                $dataPc['title'] = $title;
                $dataPc['user_name'] = $info['supplier_name'];
                $dataPc['mobile'] = $info['supplier_phone'];
                $dataPc['sale_name'] = '';
                $dataPc['maker_name'] = $info['user_name'];
                $dataPc['warehouse_name'] = $info['warehouse_name'];
                $dataPc['sure_money'] = $info['total']; //应收金额
                $dataPc['actual_money'] = $info['payment']; //应收金额
                $dataPc['remark'] = $info['des']; //备注
                $dataPc['bills_num'] = $info['number'];
                $dataPc['ctime'] = $info['create_time'];
                $dataPc['goods_list'] = $goods_list;
                $dataPc['total'] = $total_num; //数量
                $dataPc['money'] = $total_money; //金额
                $dataPc['dismoney'] = $total_dismoney; //折后金额
                $dataPc['arrears'] = ''; //欠款
                break;
            case 'cargo'://配货单
                $model = new \app\api\model\PrepareModel;
                $info = $model->getPrepareInfo($id);
                //标题
                $title = '配货单';
                //商品信息
                $goods_list = [];
                if (!empty($info['goods'])) {
                    foreach ($info['goods'] as $key => $val) {
                        $goods_list[$key]['name'] = $val['name'];
                        $goods_list[$key]['num'] = strval($val['put_number']);
                        $goods_list[$key]['price'] = strval($val['price']);
                        $goods_list[$key]['money'] = strval($val['money']);
                        $goods_list[$key]['item'] = $val['item'];
                        $goods_list[$key]['color'] = $val['color'];
                        $goods_list[$key]['size'] = $val['size'];
                        $goods_list[$key]['unit'] = $val['unit'];
                        $goods_list[$key]['discount'] = 0; //strval($val['discount']);
                        $goods_list[$key]['dismoney'] = 0; //strval($val['discount_money']);
                        $goods_list[$key]['disprice'] = 0; //strval($val['discount_price']);
                    }
                }
                $total_num = strval(array_sum(array_column($goods_list, 'num')));
                $total_money = strval(array_sum(array_column($goods_list, 'money')));
                $total_dismoney = 0; //strval(array_sum(array_column($goods_list, 'dismoney')));
                //APP数据
                $dataApp['title'] = $title;
                if (!empty($info['kid_name'])) {
                    $dataApp['info_list'][] = '客户：' . $info['kid_name'];
                }
                if (!empty($info['kid_phone'])) {
                    $dataApp['info_list'][] = '联系方式：' . $info['kid_phone'];
                }
                if (!empty($info['maker_name'])) {
                    $dataApp['info_list'][] = '制单人：' . $info['maker_name'];
                }
                if (!empty($info['warehouse_name'])) {
                    $dataApp['info_list'][] = '仓库：' . $info['warehouse_name'];
                }
                if (!empty($info['number'])) {
                    $dataApp['info_list'][] = '单号：' . $info['number'];
                }
                if (!empty($info['create_time'])) {
                    $dataApp['info_list'][] = '业务日期：' . $info['create_time'];
                }
                $dataApp['info_list'][] = '____________________________';
                $dataApp['goods_title'] = ['商品', '数量', '单价', '金额'];
                $dataApp['goods_list'] = $goods_list;
                $dataApp['footer_line'] = '____________________________';
                $dataApp['total'] = ['合计', $total_num, $total_money];
                //PC数据
                $dataPc['title'] = $title;
                $dataPc['user_name'] = $info['keeper_name'];
                $dataPc['mobile'] = '';
                $dataPc['sale_name'] = '';
                $dataPc['maker_name'] = $info['maker_name'];
                $dataPc['warehouse_name'] = $info['warehouse_name'];
                $dataPc['sure_money'] = 0; //$info['total']; //应收金额
                $dataPc['actual_money'] = 0; //$info['payment']; //应收金额
                $dataPc['remark'] = $info['des']; //备注
                $dataPc['bills_num'] = $info['number'];
                $dataPc['ctime'] = $info['create_time'];
                $dataPc['goods_list'] = $goods_list;
                $dataPc['total'] = $total_num; //数量
                $dataPc['money'] = $total_money; //金额
                $dataPc['dismoney'] = $total_dismoney; //折后金额
                $dataPc['arrears'] = ''; //欠款
                break;
            case 'purchase_retreat'://采购退货单
                $model = new \app\api\model\PurchaseModel;
                $info = $model->getReturnDetail($id);
                //标题
                $title = '采购退货单';
                //商品信息
                $goods_list = [];
                if (!empty($info['goods'])) {
                    foreach ($info['goods'] as $key => $val) {
                        $goods_list[$key]['name'] = $val['name'];
                        $goods_list[$key]['num'] = strval($val['put_number']);
                        $goods_list[$key]['price'] = strval($val['price']);
                        $goods_list[$key]['money'] = strval($val['money']);
                        $goods_list[$key]['item'] = $val['item'];
                        $goods_list[$key]['color'] = $val['color'];
                        $goods_list[$key]['size'] = $val['size'];
                        $goods_list[$key]['unit'] = $val['unit'];
                        $goods_list[$key]['discount'] = strval($val['discount']);
                        $goods_list[$key]['dismoney'] = strval($val['discount_money']);
                        $goods_list[$key]['disprice'] = strval($val['discount_price']);
                    }
                }
                $total_num = strval(array_sum(array_column($goods_list, 'num')));
                $total_money = strval(array_sum(array_column($goods_list, 'money')));
                $total_dismoney = strval(array_sum(array_column($goods_list, 'dismoney')));
                //APP数据
                $dataApp['title'] = $title;
                if (!empty($info['supplier_name'])) {
                    $dataApp['info_list'][] = '客户：' . $info['supplier_name'];
                }
                if (!empty($info['supplier_phone'])) {
                    $dataApp['info_list'][] = '联系方式：' . $info['supplier_phone'];
                }
                if (!empty($info['maker_name'])) {
                    $dataApp['info_list'][] = '制单人：' . $info['maker_name'];
                }
                if (!empty($info['warehouse_name'])) {
                    $dataApp['info_list'][] = '仓库：' . $info['warehouse_name'];
                }
                if (!empty($info['number'])) {
                    $dataApp['info_list'][] = '单号：' . $info['number'];
                }
                if (!empty($info['create_time'])) {
                    $dataApp['info_list'][] = '业务日期：' . $info['create_time'];
                }
                $dataApp['info_list'][] = '____________________________';
                $dataApp['goods_title'] = ['商品', '数量', '单价', '金额'];
                $dataApp['goods_list'] = $goods_list;
                $dataApp['footer_line'] = '____________________________';
                $dataApp['total'] = ['合计', $total_num, $total_money];
                //PC数据
                $dataPc['title'] = $title;
                $dataPc['user_name'] = $info['supplier_name']; //客户
                $dataPc['mobile'] = $info['supplier_phone']; //手机号
                $dataPc['maker_name'] = $info['maker_name']; //制单人
                $dataPc['warehouse_name'] = $info['warehouse_name']; //仓库
                $dataPc['sure_money'] = ''; //应收金额
                $dataPc['actual_money'] = $info['payment']; //应收金额
                $dataPc['bills_num'] = $info['number']; //单号
                $dataPc['ctime'] = $info['create_time']; //日期
                $dataPc['remark'] = $info['des']; //备注
                $dataPc['goods_list'] = $goods_list; //商品列表
                $dataPc['total'] = $total_num; //数量
                $dataPc['money'] = $total_money; //金额
                $dataPc['dismoney'] = $total_dismoney; //折后金额
                $dataPc['arrears'] = ''; //欠款
                break;
            case 'sale_cash'://销售前台收银历史
                $model = new \app\api\model\SaleModel;
                $info = $model->getCollectDetail($id);
                //标题
                $title = '销售前台收银详情单';
                //商品信息
                $goods_list = [];
                if (!empty($info['goods'])) {
                    foreach ($info['goods'] as $key => $val) {
                        $goods_list[$key]['name'] = $val['name'];
                        $goods_list[$key]['num'] = strval($val['put_number']);
                        $goods_list[$key]['price'] = strval($val['price']);
                        $goods_list[$key]['money'] = strval($val['money']);
                        $goods_list[$key]['item'] = $val['item'];
                        $goods_list[$key]['color'] = $val['color'];
                        $goods_list[$key]['size'] = $val['size'];
                        $goods_list[$key]['unit'] = $val['unit'];
                        $goods_list[$key]['discount'] = strval($val['discount']);
                        $goods_list[$key]['dismoney'] = strval($val['discount_money']);
                        $goods_list[$key]['disprice'] = strval($val['discount_price']);
                    }
                }
                $total_num = strval(array_sum(array_column($goods_list, 'num')));
                $total_money = strval(array_sum(array_column($goods_list, 'money')));
                $total_dismoney = strval(array_sum(array_column($goods_list, 'dismoney')));
                //APP数据
                $dataApp['title'] = $title;
                if (!empty($info['sales_name'])) {
                    $dataApp['info_list'][] = '客户：' . $info['sales_name'];
                }
                if (!empty($info['sales_phone'])) {
                    $dataApp['info_list'][] = '联系方式：' . $info['sales_phone'];
                }
                if (!empty($info['maker_name'])) {
                    $dataApp['info_list'][] = '制单人：' . $info['maker_name'];
                }
                if (!empty($info['warehouse_name'])) {
                    $dataApp['info_list'][] = '仓库：' . $info['warehouse_name'];
                }
                if (!empty($info['number'])) {
                    $dataApp['info_list'][] = '单号：' . $info['number'];
                }
                if (!empty($info['create_time'])) {
                    $dataApp['info_list'][] = '业务日期：' . $info['create_time'];
                }
                $dataApp['info_list'][] = '____________________________';
                $dataApp['goods_title'] = ['商品', '数量', '单价', '金额'];
                $dataApp['goods_list'] = $goods_list;
                $dataApp['footer_line'] = '____________________________';
                $dataApp['total'] = ['合计', $total_num, $total_money];
                //PC数据
                $dataPc['title'] = $title;
                $dataPc['user_name'] = $info['sales_name']; //客户
                $dataPc['mobile'] = $info['sales_phone']; //手机号
                $dataPc['maker_name'] = $info['maker_name']; //制单人
                $dataPc['warehouse_name'] = $info['warehouse_name']; //仓库
                $dataPc['sure_money'] = $info['total']; //应收金额
                $dataPc['actual_money'] = $info['payment']; //应收金额
                $dataPc['bills_num'] = $info['number']; //单号
                $dataPc['ctime'] = $info['create_time']; //日期
                $dataPc['remark'] = ''; //备注
                $dataPc['goods_list'] = $goods_list; //商品列表
                $dataPc['total'] = $total_num; //数量
                $dataPc['money'] = $total_money; //金额
                $dataPc['dismoney'] = $total_dismoney; //折后金额
                $dataPc['arrears'] = $info['arrear']; //欠款
                break;
            case 'outstock_out'://已出库单
                $model = new \app\api\model\OutStockModel;
                $info = $model->getInfo($id);
                //标题
                $title = '已出库单';
                //商品信息
                $goods_list = [];
                if (!empty($info['goods'])) {
                    foreach ($info['goods'] as $key => $val) {
                        $goods_list[$key]['name'] = $val['name'];
                        $goods_list[$key]['num'] = strval($val['put_number']);
                        $goods_list[$key]['price'] = strval($val['price']);
                        $goods_list[$key]['money'] = strval($val['price']);
                        $goods_list[$key]['item'] = $val['item'];
                        $goods_list[$key]['color'] = $val['color'];
                        $goods_list[$key]['size'] = $val['size'];
                        $goods_list[$key]['unit'] = $val['unit'];
                        $goods_list[$key]['discount'] = "0"; //strval($val['discount']);
                        $goods_list[$key]['dismoney'] = "0"; //strval($val['discount_money']);
                        $goods_list[$key]['disprice'] = "0"; //strval($val['discount_price']);
                    }
                }
                $total_num = strval(array_sum(array_column($goods_list, 'num')));
                $total_money = strval(array_sum(array_column($goods_list, 'money')));
                $total_dismoney = strval(array_sum(array_column($goods_list, 'dismoney')));
                //APP数据
                $dataApp['title'] = $title;
                if (!empty($info['kid_name'])) {
                    $dataApp['info_list'][] = '客户：' . $info['kid_name'];
                }
                if (!empty($info['kid_phone'])) {
                    $dataApp['info_list'][] = '联系方式：' . $info['kid_phone'];
                }
                if (!empty($info['maker_name'])) {
                    $dataApp['info_list'][] = '制单人：' . $info['maker_name'];
                }
                if (!empty($info['warehouse_name'])) {
                    $dataApp['info_list'][] = '仓库：' . $info['warehouse_name'];
                }
                if (!empty($info['number'])) {
                    $dataApp['info_list'][] = '单号：' . $info['number'];
                }
                if (!empty($info['create_time'])) {
                    $dataApp['info_list'][] = '业务日期：' . $info['create_time'];
                }
                $dataApp['info_list'][] = '____________________________';
                $dataApp['goods_title'] = ['商品', '数量', '单价', '金额'];
                $dataApp['goods_list'] = $goods_list;
                $dataApp['footer_line'] = '____________________________';
                $dataApp['total'] = ['合计', $total_num, $total_money];
                //PC数据
                $dataPc['title'] = $title;
                $dataPc['user_name'] = $info['kid_name']; //客户
                $dataPc['mobile'] = $info['kid_phone']; //手机号
                $dataPc['maker_name'] = $info['maker_name']; //制单人
                $dataPc['warehouse_name'] = $info['warehouse_name']; //仓库
                $dataPc['sure_money'] = $info['money']; //应收金额
                $dataPc['actual_money'] = $info['money']; //应收金额
                $dataPc['bills_num'] = $info['number']; //单号
                $dataPc['ctime'] = $info['create_time']; //日期
                $dataPc['remark'] = $info['des']; //备注
                $dataPc['goods_list'] = $goods_list; //商品列表
                $dataPc['total'] = $total_num; //数量
                $dataPc['money'] = $total_money; //金额
                $dataPc['dismoney'] = $total_dismoney; //折后金额
                $dataPc['arrears'] = ''; //$info['arrears']; //欠款
                break;
            case 'outstock_in'://已入库单
                $model = new \app\api\model\StorageModel;
                $info = $model->getInfo($id);
                //标题
                $title = '已入库单';
                //商品信息
                $goods_list = [];
                if (!empty($info['goods'])) {
                    foreach ($info['goods'] as $key => $val) {
                        $goods_list[$key]['name'] = $val['name'];
                        $goods_list[$key]['num'] = strval($val['put_number']);
                        $goods_list[$key]['price'] = strval($val['price']);
                        $goods_list[$key]['money'] = strval($val['price']);
                        $goods_list[$key]['item'] = $val['item'];
                        $goods_list[$key]['color'] = $val['color'];
                        $goods_list[$key]['size'] = $val['size'];
                        $goods_list[$key]['unit'] = $val['unit'];
                        $goods_list[$key]['discount'] = "0"; //strval($val['discount']);
                        $goods_list[$key]['dismoney'] = "0"; //strval($val['discount_money']);
                        $goods_list[$key]['disprice'] = "0"; //strval($val['discount_price']);
                    }
                }
                $total_num = strval(array_sum(array_column($goods_list, 'num')));
                $total_money = strval(array_sum(array_column($goods_list, 'money')));
                $total_dismoney = strval(array_sum(array_column($goods_list, 'dismoney')));
                //APP数据
                $dataApp['title'] = $title;
                if (!empty($info['supplier_name'])) {
                    $dataApp['info_list'][] = '客户：' . $info['supplier_name'];
                }
                if (!empty($info['supplier_phone'])) {
                    $dataApp['info_list'][] = '联系方式：' . $info['supplier_phone'];
                }
                if (!empty($info['maker_name'])) {
                    $dataApp['info_list'][] = '制单人：' . $info['maker_name'];
                }
                if (!empty($info['warehouse_name'])) {
                    $dataApp['info_list'][] = '仓库：' . $info['warehouse_name'];
                }
                if (!empty($info['number'])) {
                    $dataApp['info_list'][] = '单号：' . $info['number'];
                }
                if (!empty($info['create_time'])) {
                    $dataApp['info_list'][] = '业务日期：' . $info['create_time'];
                }
                $dataApp['info_list'][] = '____________________________';
                $dataApp['goods_title'] = ['商品', '数量', '单价', '金额'];
                $dataApp['goods_list'] = $goods_list;
                $dataApp['footer_line'] = '____________________________';
                $dataApp['total'] = ['合计', $total_num, $total_money];
                //PC数据
                $dataPc['title'] = $title;
                $dataPc['user_name'] = $info['supplier_name']; //客户
                $dataPc['mobile'] = $info['supplier_phone']; //手机号
                $dataPc['maker_name'] = $info['maker_name']; //制单人
                $dataPc['warehouse_name'] = $info['warehouse_name']; //仓库
                $dataPc['sure_money'] = '';//$info['money']; //应收金额
                $dataPc['actual_money'] = '';//$info['money']; //应收金额
                $dataPc['bills_num'] = $info['number']; //单号
                $dataPc['ctime'] = $info['create_time']; //日期
                $dataPc['remark'] = $info['des']; //备注
                $dataPc['goods_list'] = $goods_list; //商品列表
                $dataPc['total'] = $total_num; //数量
                $dataPc['money'] = $total_money; //金额
                $dataPc['dismoney'] = $total_dismoney; //折后金额
                $dataPc['arrears'] = ''; //$info['arrears']; //欠款
                break;
            default:
                break;
        }
        return ['pc' => $dataPc, 'app' => $dataApp];
    }

    /**
     * 客户积分列表
     * @param Request $request
     * @param ShopModel $model
     */
    public function appIntegralList(Request $request, ShopModel $model) {
        $param = $request->param();
        $page = isset($param['page']) ? $param['page'] : 1;
        $size = isset($param['size']) ? $param['size'] : 10;
        $start_time = isset($param['start_time']) ? strtotime($param['start_time']) : 0;
        $end_time = isset($param['end_time']) ? strtotime($param['end_time']) : 0;
        $member_id = isset($param['member_id']) ? $param['member_id'] : 0;
        if (empty($member_id)) {
            return $this->returnJson(500, '参数缺失');
        }
        $where = '';
        if (!empty($start_time)) {
            $where .= 'ctime > ' . $start_time . ' and ';
        }
        if (!empty($end_time)) {
            $where .= 'ctime < ' . $end_time . ' and ';
        }
        if (!empty($where)) {
            $search = 'member_id = ' . $member_id . ' and ' . mb_substr($where, 0, -5);
        } else {
            $search = 'member_id = ' . $member_id;
        }
        $list = $model->getIntegralList($page, $size, $search);
        return $this->returnJson(200, '成功', $list);
    }

    /**
     * 客户积分明细列表
     * @param Request $request
     * @param ShopModel $model
     */
    public function integralDetailList(Request $request, ShopModel $model) {
        $param = $request->param();
        $page = isset($param['page']) ? $param['page'] : 1;
        $size = isset($param['size']) ? $param['size'] : 10;
        $start_time = isset($param['start_time']) ? strtotime($param['start_time']) : 0;
        $end_time = isset($param['end_time']) ? strtotime($param['end_time']) : 0;
        $member_id = isset($param['member_id']) ? $param['member_id'] : 0;
        if (empty($member_id)) {
            return $this->returnJson(500, '参数缺失');
        }
        $where = '';
        if (!empty($start_time)) {
            $where .= 'l.ctime > ' . $start_time . ' and ';
        }
        if (!empty($end_time)) {
            $where .= 'l.ctime < ' . $end_time . ' and ';
        }
        if (!empty($where)) {
            $search = 'l.member_id = ' . $member_id . ' and ' . mb_substr($where, 0, -5);
        } else {
            $search = 'l.member_id = ' . $member_id;
        }
        $list = $model->getIntegralDetailList($member_id, $page, $size, $search);
        return $this->returnJson(200, '成功', $list);
    }

}
